// 光照管理器 - 专门处理场景光照
import * as THREE from 'three'
import { CONFIG } from '../config/constants.js'

export class LightingManager {
  constructor(scene) {
    this.scene = scene
    this.lights = new Map()
  }

  setupDefaultLighting() {
    // 环境光
    const ambientLight = new THREE.AmbientLight(
      0x404040, 
      CONFIG.DEFAULT_LIGHTING.ambientIntensity
    )
    this.addLight('ambient', ambientLight)

    // 方向光
    const directionalLight = new THREE.DirectionalLight(
      0xffffff, 
      CONFIG.DEFAULT_LIGHTING.directionalIntensity
    )
    
    const pos = CONFIG.DEFAULT_LIGHTING.directionalPosition
    directionalLight.position.set(pos.x, pos.y, pos.z)
    directionalLight.castShadow = true
    directionalLight.shadow.mapSize.width = CONFIG.RENDERER.SHADOW_MAP_SIZE
    directionalLight.shadow.mapSize.height = CONFIG.RENDERER.SHADOW_MAP_SIZE
    
    this.addLight('directional', directionalLight)
  }

  addLight(name, light) {
    if (this.lights.has(name)) {
      this.removeLight(name)
    }
    
    this.lights.set(name, light)
    this.scene.add(light)
  }

  removeLight(name) {
    const light = this.lights.get(name)
    if (light) {
      this.scene.remove(light)
      this.lights.delete(name)
    }
  }

  updateLightIntensity(name, intensity) {
    const light = this.lights.get(name)
    if (light) {
      light.intensity = intensity
    }
  }

  updateLightPosition(name, x, y, z) {
    const light = this.lights.get(name)
    if (light && light.position) {
      light.position.set(x, y, z)
    }
  }

  updateLightColor(name, color) {
    const light = this.lights.get(name)
    if (light) {
      light.color.setHex(color)
    }
  }

  dispose() {
    this.lights.forEach((light, name) => {
      this.removeLight(name)
    })
  }
}